﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace EntityServices
{
    /// <summary>
    /// Summary description for SinhVien
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SinhVien : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public List<SINH_VIEN> traCuuSinhVien(SINH_VIEN infoSV)
        {
            HTQLGiaoDucDataContext dtContext = new HTQLGiaoDucDataContext();


            var students = dtContext.SINH_VIENs.Where(temp => temp.HOTEN.Contains(infoSV.HOTEN) && temp.MASOSINHVIEN.Contains(infoSV.MASOSINHVIEN));
            List<SINH_VIEN> ds = new List<SINH_VIEN>();

            foreach (SINH_VIEN s in students)
            {
                SINH_VIEN sv = new SINH_VIEN { SINHVIENID = s.SINHVIENID, HOTEN=s.HOTEN, CMND=s.CMND, DIACHI=s.DIACHI, KHOAID=s.KHOAID, MASOSINHVIEN = s.MASOSINHVIEN, NGAYSINH =s.NGAYSINH };
                ds.Add(sv);
            }
            return ds;
        }
        [WebMethod]
        public void ThemSinhVien(SINH_VIEN sv)
        {
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();
            db.SINH_VIENs.InsertOnSubmit(sv);
            db.SubmitChanges();
        }
        [WebMethod]
        public void XoaSinhVien(string mssv)
        {
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();
            var query = from sv in db.SINH_VIENs
                        where sv.MASOSINHVIEN == mssv
                        select sv;
            db.SINH_VIENs.DeleteAllOnSubmit(query);
            db.SubmitChanges();
        }
        [WebMethod]
        public void CapNhatSinhVien(SINH_VIEN sv)
        {
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();
            int masv = db.SINH_VIENs.Single(a => a.MASOSINHVIEN == sv.MASOSINHVIEN).SINHVIENID;

            SINH_VIEN tempt =  db.SINH_VIENs.Single(a => a.SINHVIENID == masv);
            tempt.HOTEN = sv.HOTEN;
            tempt.KHOA = sv.KHOA;
            tempt.NGAYSINH = sv.NGAYSINH;
            tempt.CMND = sv.CMND;
            tempt.DIACHI = sv.DIACHI;
            db.SubmitChanges();
        }
        [WebMethod]
        public bool KiemTraSVHopLe(string mssv)
        {
            int tt = new int();
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();
            int masv = db.SINH_VIENs.Single(a => a.MASOSINHVIEN == mssv).SINHVIENID;

            var query = from sv in db.SINH_VIENs
                        where sv.SINHVIENID == masv
                        select sv;
            foreach(SINH_VIEN sv in query)
            {
                tt = (int)sv.TINHTRANG;
            }

            if (tt == 1)
            {
                return true;
            }
            else
                return false;
        }

    }
}
